import Vue from 'vue'
import VueRouter from 'vue-router'
import defaultPage from '@/layout/default.vue'
import goodsList from '@/page/goodsList.vue'
import home from '@/views/Home.vue'
import Index from '@/page/index.vue'
import changeCity from '@/page/changeCity.vue'
import Login from '@/page/login.vue'
import Register from '@/page/register.vue'
import blankPage from '@/layout/blank.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'default',
    component: defaultPage,
    redirect:'/index',
    children:[{
      path:'index',
      name:'index',
      component:Index
    },
    {
      path:'changeCity',
      name:'changeCity',
      component:changeCity
    },
    {
      path:'home',
      name:'home',
      component:home
    },
    {
      path: 's/:name',
      name: 'goods',
      component: goodsList
    }
  ]

  },
  {
    path:'/p/:detail',
    name:'detail',
    component:()=>import('@/page/productDeatil')
  },
  {
    path: '/blank',
    name: 'blank',
    component: blankPage,
    children: [{
      path: 'login',
      name: 'login',
      component: Login
    }, {
      path: 'register',
      name: 'register',
      component: Register
    }]
  },
  {
    path:'/notFound',
    name:'notFound',
    component:home
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {  
    next({ name: 'notFound' });
    return;
  }
  next();
})

export default router
